#import "RNPrintListItemCell.h"
#import "RNConstants.h"

static NSUInteger const titleLeftPadding = 10;
static NSUInteger const notesBottomPadding = 2;
static NSUInteger const notesFontSize = 10;
static NSUInteger const checkboxLeftPadding = 5;
static NSUInteger const checkboxTopPadding = 4;
static NSUInteger const checkboxEdgeLength = 10;
static NSUInteger const countLeftPadding = 10;
static NSUInteger const countLength = 10;

@implementation RNPrintListItemCell

@synthesize titleAttributes = titleAttributes_;
@synthesize notesAttributes = notesAttributes_;

-(id)initTextCell:(NSString *)title {
    if (!(self = [super initTextCell:title]))
        return nil;
    
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setValue:[NSFont fontWithName:@"Palatino" size:12.0f] forKey:NSFontAttributeName];
    [self setTitleAttributes:attributes];
    
    attributes = [NSMutableDictionary dictionary];
    [attributes setValue:[NSFont fontWithName:@"Palatino" size:notesFontSize] forKey:NSFontAttributeName];
    [attributes setValue:[NSColor colorWithCalibratedWhite:0.5 alpha:1.0] forKey:NSForegroundColorAttributeName];
    [self setNotesAttributes:attributes];
    
    return self;
}

-(void)setObjectValue:(id)obj {
    if (![obj isKindOfClass:[NSDictionary class]])
        return;
    [super setObjectValue:obj];
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    // Draw count
    NSAttributedString *count = [self attributedCount];
    if (count)
        [count drawInRect:[self countRectForBounds:cellFrame]];
    
    // Draw checkbox
    if (count) {
        NSRect checkboxRect = [self checkboxRectForBounds:cellFrame];
        [[NSColor blackColor] setStroke];
        [[NSBezierPath bezierPathWithRect:checkboxRect] stroke];
    }
    
    // Draw title
    NSString *titleString = [[self objectValue] valueForKey:RNTitleKey];
    // Include tags
    NSArray *tags = [[self objectValue] valueForKey:@"tags"];
    if (tags && ![tags isKindOfClass:[NSNull class]]) {
        titleString = [titleString stringByAppendingFormat:@"    (%@", [tags objectAtIndex:0]];
        for (NSUInteger tagIndex = 1; tagIndex < [tags count]; tagIndex++) {
            NSString *tag = [tags objectAtIndex:tagIndex];
            titleString = [titleString stringByAppendingFormat:@", %@", tag];
        }
        titleString = [titleString stringByAppendingString:@")"];
    }
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:titleString attributes:[self titleAttributes]];
    [title drawInRect:[self titleRectForBounds:cellFrame]];
    
    // Draw notes
    NSAttributedString *notes = [self attributedNotes];
    if (!notes)
        return;
    [notes drawInRect:[self notesRectForBounds:cellFrame]];
}

-(NSRect)titleRectForBounds:(NSRect)cellFrame {
    NSRect titleRect = cellFrame;
    CGFloat xAdjustment = checkboxLeftPadding + checkboxEdgeLength + titleLeftPadding + countLength + countLeftPadding;
    titleRect.origin.x += xAdjustment;
    titleRect.size.width -= xAdjustment;
    return titleRect;
}

-(NSRect)notesRectForBounds:(NSRect)bounds {
    CGFloat notesHeight = [[self attributedNotes] size].height;
    CGFloat xAdjustment = checkboxLeftPadding + checkboxEdgeLength + titleLeftPadding + countLength + countLeftPadding;
    NSRect notesRect = NSMakeRect(NSMinX(bounds) + xAdjustment, NSMaxY(bounds) - notesBottomPadding - notesHeight, NSWidth(bounds) - xAdjustment, notesHeight);
    return notesRect;
}

-(NSRect)checkboxRectForBounds:(NSRect)bounds {
    return NSMakeRect(NSMinX(bounds) + checkboxLeftPadding, NSMinY(bounds) + checkboxTopPadding, checkboxEdgeLength, checkboxEdgeLength);
}

-(NSRect)countRectForBounds:(NSRect)bounds {
    NSRect countRect = bounds;
    CGFloat xAdjustment = checkboxLeftPadding + checkboxEdgeLength + countLeftPadding;
    countRect.origin.x += xAdjustment;
    countRect.size.width = countLength;
    return countRect;
}

-(NSAttributedString *)attributedNotes {
    NSString *notesString = [[self objectValue] objectForKey:@"notes"];
    if (!notesString || [notesString isKindOfClass:[NSNull class]])
        return nil;
    return [[NSAttributedString alloc] initWithString:notesString attributes:[self notesAttributes]];
}

-(NSAttributedString *)attributedCount {
    NSNumber *count = [[self objectValue] objectForKey:@"count"];
    if (!count || [count isKindOfClass:[NSNull class]])
        return nil;
    return [[NSAttributedString alloc] initWithString:[count stringValue] attributes:[self titleAttributes]];
}

@end
